//
//  Friend.swift
//  app_rn_frontend
//
//  Created by 鱼涛 on 2017/12/13.
//  Copyright © 2017年 Facebook. All rights reserved.
//

import Foundation
import WCDBSwift

enum Gender: Int {
    case unknown = 0
    case male = 1
    case female = 2
}

extension Gender: ColumnCodable,LiteralValueConvertible {
    init?(with value: FundamentalValue) {
        let v = Int(value.int32Value)
         self = Gender(rawValue: v) ?? Gender.unknown
    }
    
    func archivedValue() -> FundamentalValue {
        return FundamentalValue(self.rawValue)
    }
    
    static var columnType: ColumnType {
        return .integer64
    }
}

enum Role:Int {
  case normal = 0 //一般联系人,非家族联系人
  case member = 1 //家族成员
  case direct = 2 //下级
  case parent = 3 //上级
  
  func getDisplay() -> String {
    switch self {
    case .normal:
      return ""
    case .member:
      return "家族"
    case .direct:
      return "下级"
    case .parent:
      return "上级"
    }
  }
}

extension Role: ColumnCodable,LiteralValueConvertible {
    
    init?(with value: FundamentalValue) {
        let v = Int(value.int32Value)
        self = Role(rawValue: v) ?? Role.normal
    }
    
    func archivedValue() -> FundamentalValue {
        return FundamentalValue(self.rawValue)
    }
    
    static var columnType: ColumnType {
        return .integer64
    }
}

//联系人
final class Contact: TableCodable {
    static let table = "contact"

    var userId: Int = 0                  //联系人uId
    var name: String = ""                  //联系人名称
    var remarkName: String? = nil          //联系人备注名，客户端设置，同步到服务端
    var createTime: Date = Date()          //创建时间，一经创建，不再更改
    var lastUpdTime: Date = Date()         //最近更新时间，客户端通过此字段，判断在某个时间点是否需要更新用户信息，更新用户信息后，更新此字段
    var gender: Gender = Gender.unknown    //性别
    var avatar: String? = nil              //头像
    var friend: Bool = false               //好友标志，为true显示在好友列表
    var family: Bool = false               //家族标志，为true显示在家族列表中
    var canAddGroup: Bool = false          //是否可以建群标志，为true时，在建群操作列表中显示，无论是否为好友，通常家族成员和好友这个字段都为true
    var forceUpdate: Bool = false          //强制更新标志，如果此标志为true，将忽略最近更新时间，直接更新数据
    var me: Bool = false
    var deleted: Int = 0                 //删除标志
    var role:Role = .normal                //联系人角色

    @objc public func availableName() -> NSString {
        return NSString(string: self.remarkName ?? self.name)
    }

    static func addNewFriend(userId: Int, name: String, remarkName: String? = nil, gender: Gender = .unknown, avatar: String? = nil) -> Contact {
        let friend = Contact()
        friend.userId = userId
        friend.name = name
        friend.remarkName = remarkName
        friend.gender = gender
        friend.avatar = avatar
        friend.friend = true
        friend.canAddGroup = true
        return friend
    }

    static func initFamilyMember(userId: Int, name: String, remarkName: String? = nil, gender: Gender = .unknown, avatar: String? = nil) -> Contact {
        let member = Contact()
        member.userId = userId
        member.name = name
        member.remarkName = remarkName
        member.gender = gender
        member.avatar = avatar
        member.family = true
        member.canAddGroup = true
        return member
    }

    enum CodingKeys: String, CodingTableKey {
        typealias Root = Contact
        case userId = "user_id"
        case name
        case remarkName = "remark_name"
        case createTime = "create_time"
        case lastUpdTime = "last_upd_time"
        case gender
        case avatar
        case friend
        case family
        case canAddGroup
        case forceUpdate
        case me
        case deleted
        case role
        
        static let objectRelationalMapping = TableBinding(CodingKeys.self) {
            BindColumnConstraint(.userId, isPrimary: true, isAutoIncrement: false)
            BindColumnConstraint(.role, defaultTo: 0)
        }
    }
}
